module vlib.util.Ini;

import vlib.Types;

struct vIni {
	char[]	fcgi_ip 	= "127.0.0.1" ;
	int		fcgi_port	= 1983 ;
	int		fcgi_max_process	= 3 ;
	
	char[]	php		= "" ;
	char[]	php_args	= null ;
	char[][]	dns_ip	= []  ;
	char[][char[]]		env ;
	
	alias void delegate(char[], char[]) ParserDg ;
	
	void Init() {
		wchar[512]	path;
		int i	= GetModuleFileNameW(null, path.ptr, path.length);
		char[1024]	tmp ;
		char[] _path	= Utf.toString(path[0..i], tmp);
		foreach( ref c; _path ){
			if( c is '\\' ) c = '/' ;
		}
		i	= rfind( _path, '/') ;
		assert(i !is _path.length);
		
		int j	= rfind(_path, '.');
		assert(j !is _path.length);
		vLib.app	=  _path[i+1..j].dup ;
		vLib.dir	= _path[0..i+1].dup ;

		_path[++i]	= '\0' ;
		Environment.cwd(_path[0..i]);
		
		scope ini_file	=  vLib.app ~ ".ini" ;
		if( !Pth.exists(ini_file) ){
			return ;
		}
		scope dat	= cast(char[]) File.get(ini_file) ;
		vTok tz;
		tz(dat, '\n');
		vToken _tz ;
		
		ParserDg	dg	= &parser_fcgi ;
		foreach( _ln ; tz ){
			char[] ln	= Txt.trim(_ln) ;
			if( ln.length is 0 ) {
				continue ;
			}
			int i ;
			i	= find(ln, '#') ;
			ln	= ln[0..i] ;
			if( ln.length is 0 ) {
				continue ;
			}
			i	= find(ln, ';') ;
			ln	= ln[0..i] ;
			if( ln.length is 0 ) {
				continue ;
			}
			if( ln[0] is '[' && ln[$-1] is ']' ) {
				switch(  ln[1..$-1] ){
					case "fcgi" :
						dg	= &parser_fcgi ;
						break ;
					case "Environment":
						dg	= &parser_env ;
						break ;
					default:
						lg("parser_unknow:{}",  ln[1..$-1] ) ;
						dg	= &parser_unknow ;
				}
				continue ;
			}
			_tz(ln);
			char[] key	= Txt.trim( _tz.tok('=') ) ;
			char[] val		= Txt.trim(_tz.remain) ;
			if( key.length is 0 || val.length is 0 ) {
				continue ;
			}
			dg(key, val);
		}
		
		if( dns_ip.length is 0 ) {
			dns_ip	~= "8.8.4.4" ;
		}
		
		if( php is null || php.length is 0 ) {
			lg("php is not find" ) ;
			vLib.Exit(1) ;
		}
		
		// lg("env: {}", env) ;
	}
	
	void parser_env(char[] key, char[] val) {
		env[ key.dup ] = val.dup ;
	}
	
	void parser_unknow(char[] key, char[] val){
		
	}
	
	void parser_fcgi(char[] key, char[] val){
		switch(key) {
			case "dns_ip":
				ubyte[4] ipv ;
				if( vDns.isIp(val, ipv) ) {
					dns_ip	~= val.dup ;
				}
				break;
			case "fcgi_ip":
				ubyte[4] ipv ;
				if( vDns.isIp(val, ipv) ) {
					fcgi_ip	= val.dup ;
				}
				break;
			case "fcgi_port":
				int _fcgi_port	= Int.toInt(val) ;
				if( _fcgi_port > 80 && _fcgi_port <= 65535 ) {
					fcgi_port	= _fcgi_port ;
				}
				break;
			case "fcgi_max_process":
				int _max_cgi	= Int.toInt(val) ;
				if( _max_cgi > 0 && _max_cgi <= 0xf ) {
					fcgi_max_process	= _max_cgi ;
				}
				break;
			case "php":
				if( Pth.exists(val) ) {
					php	= val.dup ;
				} else {
					lg("php file '{}' is not exists", val ) ;
				}
				break;
			default:
				lg("'{}' = '{}' ",  key, val ) ;
				break ;
		}
	}
}

static vIni	_G ;